// pages/cultrueConpous/cultrueCouponsDetail/index.js
const common = require("../../../../public.js")
const App = getApp()
Page({
    data: {
        userInfo: '',
        token: '',
        dcId: '',
        couponInfo: '', // 优惠券详细信息
        storeList: [], // 附近商家信息
        showAllAddr: false,
        imgCode: '',
        code: '',
        lon: 0,
        lat: 0,
        showHelpArea: false,
        helpState: 0,
        helpInfo: null,
        couponNo: null,
        needsHelpNum: 0,
        helpNum: 0,
        isLoading: false,
        navH: App.globalData.navHeight
    },

    onLoad: function (opt) {
        const that = this
        this.data.couponNo = opt.no
        console.log(opt, "6666666666666666666")
        common.getLocalUserInfo(function (success, userInfo) {
            if (success) {
                getApp().getLocation(function (data) {
                    that.data.lon = data.longitude
                    that.data.lat = data.latitude
                    that.setData({
                        userInfo: userInfo.user,
                        token: userInfo.token,
                        dcId: opt.dcId,
                        from: opt.from ? opt.from : null,
                    }, function () {
                        that.getCityDisCouponInfo()
                    })
                })
            }
        }, true)
    },
    back() {
        wx.navigateBack({
            delta: 1,
        })
    },
    // onReady() {
    //   this.createCode()
    // },
    getCityDisCouponInfo() {
        let that = this
        let tempJson = {
            xpoint: that.data.lon,
            ypoint: that.data.lat,
            dcId: that.data.dcId
        }
        this.setData({
            isLoading: true
        })
        common.post('/cityDisCoupon/getCityDisCouponInfo', tempJson, function (res) {
            if (res.status === 200) {
                that.setData({
                    couponInfo: res.data.couponInfo,
                    storeList: res.data.storeList,
                })
                if (res.data.couponInfo && res.data.couponInfo.helpFlag &&
                    res.data.couponInfo.helpFlag == 1 && that.data.couponNo) {
                    that.getCityDiscountCouponInfoAndHelp(that.data.couponNo)
                }
            } else {
                common.showRequestErrMsg(res)
            }
        }, function () {
            that.setData({
                isLoading: false
            })
        })
    },
    // toStoreList() {
    //   wx.navigateTo({
    //     url: '/pages/cultureCoupons/useableStore/index?couponId=' + this.data.couponInfo.id,
    //   })
    // },
    showAllStores() {
        this.setData({
            showAllAddr: true
        })
    },
    onClose() {
        this.setData({
            showAllAddr: false
        })
    },
    clickStore(e) {
        // console.log(e.currentTarget.dataset.item.id,"111111")
        const item = e.currentTarget.dataset.item
        const storeTypeName = e.currentTarget.dataset.item.storeTypeName
        const id = item.id
        this.shopInfo(item)
      
    },
    // 店铺判断是否外卖
    shopInfo(item) {
        const storeTypeName = item.storeTypeName
        const id = item.id
        var that = this
        var tempJson = {
            storeId: item.id,
            // storeId:33,
            xpoint: this.data.lat,
            ypoint: this.data.lon,
            xcFlag: '0' //是否含有携程门票 0不含有
        }
        common.get('/store/mainPageInfo', tempJson, res => {
            that.setData({
                industryType: res.data.store.industryType
            }, () => {
                if (this.data.industryType == 2) {
                    wx.navigateTo({
                        url: `/packageC/pages/activeBei/activeBei?id=${item.id}&name=${item.storeName}`,
                    })
                    return
                }
                if (storeTypeName === '景区景点') {
                    wx.navigateTo({
                        url: '/pages/ScenicArea/ScenicAreaMainPage/index?id=' + id + '&moduleType=1',
                    })
                } else if (storeTypeName === '文化馆' || storeTypeName === '美术馆' || storeTypeName === '博物馆') {
                    wx.navigateTo({
                        url: '/pages/ScenicArea/ScenicAreaMainPage/index?id=' + id + '&moduleType=2',
                    })
                } else {
                    common.gotoStore(item)
                }
            })
        })
    },
    /**
     * 领取惠民券成功回调
     * @param {object} e 回调参数
     */
    getCouponFinish(e) {
        if (this.data.couponInfo.helpFlag == 1 && e && e.detail && e.detail.couponNo) {
            // 助力券领取成功回调
            this.data.couponNo = e.detail.couponNo
            this.getCityDiscountCouponInfoAndHelp(this.data.couponNo)
        } else {
            // 非助力券领取成功回调
            this.getCityDisCouponInfo()
        }
    },
    getCityDiscountCouponInfoAndHelp(couponNo) {
        const that = this
        const param = {
            couponNo,
            userId: that.data.userInfo.id,
        }
        common.post('/cityDisCoupon/getCityDiscountCouponInfoAndHelp', param, function (res) {
            if (res.status === 200) {
                const helpList = res.data.helpList // 帮助列表
                const totalNum = res.data.helpNum
                const helpNum = helpList ? helpList.length : 0
                const needsHelpNum = totalNum - helpNum
                that.setData({
                    needsHelpNum,
                    helpNum,
                    helpInfo: res.data,
                    showHelpArea: true,
                    helpState: 1
                })
            } else {
                common.showRequestErrMsg(res)
            }
        })
    },
    onShareAppMessage: function (res) {
        let title = this.data.couponInfo.couponName
        let imageUrl = 'https://feiyangimage.oss-cn-shanghai.aliyuncs.com/wxApplets/xfj/shareCoupon.jpg'
        let path = '/pages/cultureCoupons/cultureCouponsDetail/index?dcId=' + this.data.couponInfo.id
        if (res.target && res.target.dataset && res.target.dataset.type == 'help') {
            title = '我正在抢1亿惠民消费券，快帮我助力吧！'
            path = '/pages/cultureCoupons/powerCoupons/index?couponNo=' + this.data.couponNo
        }
        console.log('path为', path)
        return {
            title,
            imageUrl,
            path
        }
    }
})